package org.geekbang.resilience4j.bulkhead;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.utils.StringUtils;

/**
 * resilience4j constants.
 *
 * @author leo
 * @date 2021/9/8
 * @since 1.0.0
 */
public interface BulkheadConstants extends CommonConstants {

    /**
     * bulk head pattern default semaphore.
     * @see Pattern
     */
    String PATTERN = "pattern";

    /**
     * max wait mill seconds default
     * {@link io.github.resilience4j.bulkhead.BulkheadConfig#DEFAULT_MAX_WAIT_DURATION}
     * @see Pattern#SEMAPHORE
     */
    String MAX_WAIT_MILL_SECONDS = "maxWaitMillSeconds";

    /**
     * max concurrent calls default
     * {@link io.github.resilience4j.bulkhead.BulkheadConfig#DEFAULT_MAX_CONCURRENT_CALLS}
     * @see Pattern#SEMAPHORE
     */
    String MAX_CONCURRENT_CALLS = "maxConcurrentCalls";

    /**
     * Configures the max thread pool size default {@link Runtime#availableProcessors()}
     * @see Pattern#THREAD_POOL
     */
    String MAX_THREAD_POOL_SIZE = "maxThreadPoolSize";

    /**
     * Configures the core thread pool size default {@link Runtime#availableProcessors()} - 1
     */
    String CORE_THREAD_POOL_SIZE = "coreThreadPoolSize";

    /**
     * Configures the capacity of the queue default 100.
     */
    String QUEUE_CAPACITY = "queueCapacity";

    /**
     *When the number of threads is greater than the core,
     * this is the maximum time that excess idle threads
     * will wait for new tasks before terminating default 20ms.
     */
    String KEEP_ALIVE_DURATION_MILLS = "keepAliveDuration";

    String SEMAPHORE_FILTER_NAME = "resilience4j.semaphore.filter";

    String THREAD_POOL_FILTER_NAME = "resilience4j.thread.pool.filter";

    enum Pattern{
        /**
         * semaphore pattern.
         */
        SEMAPHORE,

        /**
         * thread pool pattern.
         */
        THREAD_POOL;

        public static Pattern of(String name){
            try {

                if (StringUtils.isBlank(name)){
                    return SEMAPHORE;
                }
                return Pattern.valueOf(name.toUpperCase());
            } catch (IllegalArgumentException e) {
                return SEMAPHORE;
            }
        }
    }
}
